package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.repository.UsersRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @description:  测试
* @author: zhouzhong
* @date: 2021/07/10 12:13
* @path: com.example.demo.controller.TestController
* @version: 1.0
*/
@RestController
@RequestMapping(value = "/test")
public class TestController {
    @Autowired
    private UsersRepository usersService;

    //查询
    @RequestMapping(value = "/query")
    public Map query(){
        Map map_res = new HashMap();
        User search = new User();
//        search.setState("1");
        Example<User> example =Example.of(search);
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Pageable page = PageRequest.of(0,5,sort);
        Page pages = usersService.findAll(example,page);
        List<User> content = pages.getContent();
        map_res.put("total",pages.getTotalElements());
        map_res.put("list",content);
        return map_res;
    }

    //添加一个user
    @RequestMapping(value = "/add_one_user")
    public Map add_one_boy(){
        Map map_res = new HashMap();
        try{
            User user = new User();
            user.setUsername("Username");
            user.setPassword("Password");
            user = usersService.save(user);
            map_res.put("res",user);
        }catch(Exception e){
            System.out.println("添加user异常："+e.getMessage());
        }
        return map_res;
    }

}
